<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Services\Tools\CommonService;
use Illuminate\Http\Request;                //引用http请求类
use App\Exceptions\BasicException;          //引入框架异常处理类
use Illuminate\Support\Facades\Hash;        //引入hash
use App\Services\Cache\AdminCacheService;   //admin缓存
use App\Services\Tools\JwtService;          //引入JWT服务
use App\Services\Tools\FileService;         //引入文件处理类
use App\Response\Response;
use App\Models\MainDB\GiveGiftDay;

class GiftController extends BaseController
{

    /**
     * @api {post} /gift/list 礼物列表
     * @apiVersion 0.1.0
     * @apiName 礼物列表
     * @apiGroup 礼物管理
     *
     * @apiParam {string} sold   是否上架：1上架 2下架，非必传
     * @apiParam {string} type    分类 ：1房间礼物  2 游戏礼物，非必传
     * @apiParam {string} name    礼物名字，非必传
     * @apiParam {string} blind_box_type    盲盒礼物类型：1银 2铜 3金，非必传
     * @apiParam {number} page     页数，非必传
     * @apiParam {number} perPage     页数，非必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "list": [
     * {
     * "id(主键id)": 161,
     * "name（名字）": "公主の梦",
     * "picture（图片）": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5e63d1528eb2e.png",
     * "price（价格）": 520000,
     * "special（特效）": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5e63d1584d2cb.svga",
     * "sort（排序）": 0,
     * "type（礼物分类）": "1",
     * "type_name（礼物名字）": "1",
     * "sold"（是否下架  1.否 2.是）: 2,
     * "gift_frame": {
     * "id": 17,
     * "name": "房间礼物0.1档",
     * "condition1": 1000,
     * "condition2": 4000,
     * "condition3": 12000,
     * "frame1": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/gold.png",
     * "frame2": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/silver.png",
     * "frame3": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/copper.png",
     * "add_time": 0,
     * "deleted": 0
     * }
     * }
     * ],
     * "meta": {
     * "page（页数）": 1,
     * "limit（每页数量）": 10,
     * "next（下一页）": "http://admin.rh/gift/list?=2",
     * "total（总数）": 37
     * }
     * }
     * }
     *
     */
    public function giftList(Request $request)
    {
        $sold = $request->input('sold', 0);
        $type = $request->input('type', 0);
        $name = $request->input('name', '');
        $blind_box_type = $request->input('blind_box_type', 0);
        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $where = [];
        if ($name) {
            $where[] = ['name', 'like', "%{$name}%"];
        }
        if ($type) {
            $where[] = ['type', $type];;
        }
        if ($sold) {
            $where[] = ['sold', $sold];
        }
        if ($blind_box_type) {
            $where[] = [function ($query) use ($blind_box_type) {
                if($blind_box_type==1){
                    $query->where('type', 5);
                }
                if($blind_box_type==2){
                    $query->where('type', 4);
                }
                if($blind_box_type==3){
                    $query->where('type', 13);
                }
            }];
        }
        $giftList = app('repo_gift')->giftList($where, $page, $perPage);

        if ($giftList) {
            $type_detail=app('repo_gift_type')->giftTypeList([['id','>',0]]);
            foreach ($giftList as $key => $value) {
                foreach ($type_detail as $key2 => $value2) {
                    if ($value['type']==$value2['id']) {
                        $value['type_name'] = $value2['name'];
                    } else {
                        $value['type_name'] = "未知类型";
                    }
                    $value['type_name']=$value['type']==$value2['id']?$value2['name']:"未知类型";
                }
            }
        }

        return Response::sendList($giftList, [], [], $code = 200);
    }


    /**
     * @api {post} /gift/add 添加礼物
     * @apiVersion 0.1.0
     * @apiName 添加礼物
     * @apiGroup 礼物管理
     *
     * @apiParam {string} name   礼物名称，必传
     * @apiParam {string} type    礼物分类 ，必传
     * @apiParam {file} picture    礼物图片，必传
     * @apiParam {file} special     礼物特效，必传
     * @apiParam {string} price     礼物价格，必传
     * @apiParam {number} sort     排序，非必传
     * @apiParam {number} frame_id     边框id，非必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93020  添加礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93020,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function addGift(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'name'    => 'required|string|max:255',
                'type'    => 'required|numeric',
                'cardiac'    => 'required|numeric',
                'picture' => 'required',
//                'special' => 'required',
                'frame_id' => 'required',
                'price'   => 'required|numeric|min:0|max:2100000000',
                'sort'   => 'filled|numeric|min:0|max:2100000000',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));
        $sort = $request->input('sort', 0);
        $param = $request->only(['name', 'type', 'price', 'frame_id','picture','special', 'cardiac']);
        $param['sort'] = $sort;

        app('repo_gift')->addGift($param);

        return Response::sendData(true);
    }

    /**
     * @api {post} /gift/save 编辑礼物
     * @apiVersion 0.1.0
     * @apiName 编辑礼物
     * @apiGroup 礼物管理
     *
     * @apiParam {number} id   主键ID，必传
     * @apiParam {string} name   礼物名称，必传
     * @apiParam {string} type    礼物分类 ，必传
     * @apiParam {file} picture    礼物图片，必传
     * @apiParam {file} special     礼物特效，非必传
     * @apiParam {string} price     礼物价格，必传
     * @apiParam {number} sort     排序，非必传
     * @apiParam {number} frame_id     边框id，非必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93024  编辑礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93024,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function saveGift(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id'      => 'required|numeric',
                'name'    => 'required|string|max:255',
                'type'    => 'required|numeric',
                'cardiac'    => 'required|numeric',
                'picture' => 'required',
//                'special' => 'required',
                'frame_id' => 'required',
                'price'   => 'required|numeric|min:0|max:2100000000',
                'sort'   => 'filled|numeric|min:0|max:2100000000',
            ]
        );

        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));
        $sort = $request->input('sort', 0);
        $param = $request->only(['id', 'name', 'type', 'price', 'frame_id','picture','special', 'cardiac']);
        $param['sort'] = $sort;
        app('repo_gift')->saveGift($param);

        return Response::sendData(true);
    }

    /**
     * @api {post} /gift/del 删除礼物
     * @apiVersion 0.1.0
     * @apiName 删除礼物
     * @apiGroup 礼物管理
     *
     * @apiParam {string} ids   删除的礼物主键（多个用逗号隔开），必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93021  添加礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93021,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function delGift(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }
        $ids = $request->input('ids');
        $arr_ids = explode(',', $ids);
        app('repo_gift')->delGift($arr_ids);

        return Response::sendData(true);
    }

    /**
     * @api {post} /gift/up_or_down 上架或者下架礼物
     * @apiVersion 0.1.0
     * @apiName 上架或者下架礼物
     * @apiGroup 礼物管理
     *
     * @apiParam {string} id   礼物主键，必传
     * @apiParam {string} sold   类型：1上架 2下架，必传
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93022  上架或者下架礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93022,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function UpOrDown(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id'   => 'required|numeric',
                'sold' => 'required|numeric|in:1,2',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }

        $id = $request->input('id');
        $sold = $request->input('sold');
        app('repo_gift')->UpOrDown($id, $sold);

        return Response::sendData(true);
    }


    /**
     * @api {post} /gift/detail 礼物详情
     * @apiVersion 0.1.0
     * @apiName 礼物详情
     * @apiGroup 礼物管理
     *
     * @apiParam {string} id   礼物主键，必传
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "id(主键id)": 161,
     * "name（名字）": "公主の梦",
     * "picture（图片）": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5e63d1528eb2e.png",
     * "price（价格）": 520000,
     * "special（特效）": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5e63d1584d2cb.svga",
     * "sort（排序）": 0,
     * "type（礼物分类）": "房间礼物",
     * "sold"（是否下架  1.否 2.是）: 2,
     * "gift_frame": {
     * "id": 17,
     * "name": "房间礼物0.1档",
     * "condition1": 1000,
     * "condition2": 4000,
     * "condition3": 12000,
     * "frame1": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/gold.png",
     * "frame2": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/silver.png",
     * "frame3": "https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/custom/gift_frame/copper.png",
     * "add_time": 0,
     * "deleted": 0
     * }
     * }
     * }
     *
     */

    public function giftDetail(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) {
            throw new BasicException(10001);
        }

        $id = $request->input('id');
        $detail = app('repo_gift')->giftDetail($id);
        $gift_type = app('repo_gift_type')->GiftTypeDetail($detail['type']);
        $detail['type'] = $gift_type->name;

        return Response::sendData($detail);
    }


}
